package com.dhh.sky.activity;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.FragmentActivity;
import android.view.KeyEvent;
import android.view.Window;
import android.widget.Toast;

import com.dhh.sky.R;
import com.dhh.sky.SDAApplication;
import com.korovyansk.android.slideout.SlideoutHelper;
import com.microsoft.live.LiveAuthClient;
import com.microsoft.live.LiveAuthException;
import com.microsoft.live.LiveAuthListener;
import com.microsoft.live.LiveConnectSession;
import com.microsoft.live.LiveStatus;

public class MenuActivity extends FragmentActivity{
    private LiveAuthClient mAuthClient;
    private ProgressDialog progressDialog;

	@Override
	public void onCreate(Bundle savedInstanceState){
	    super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
	    mSlideoutHelper = new SlideoutHelper(this);
	    mSlideoutHelper.activate();
	    getSupportFragmentManager().beginTransaction().add(com.korovyansk.android.slideout.R.id.slideout_placeholder, new MenuFragment(), "menu").commit();
		mAuthClient = ((SDAApplication) getApplication()).getAuthClient();
		mSlideoutHelper.open();
	}

	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if(keyCode == KeyEvent.KEYCODE_BACK){
			mSlideoutHelper.close();
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}


	public SlideoutHelper getSlideoutHelper(){
		return mSlideoutHelper;
	}
	
	public void logout(){
		progressDialog = ProgressDialog.show(this, "", getString(R.string.msg_waite));
		mAuthClient.logout(new LiveAuthListener(){
			@Override
			public void onAuthComplete(LiveStatus status, LiveConnectSession session, Object userState) {
				handler.sendEmptyMessage(0);
				Intent intent = new Intent(MenuActivity.this,SDAActivity.class);
                startActivity(intent);
			}

			@Override
			public void onAuthError(LiveAuthException exception, Object userState) {
				handler.sendEmptyMessage(0);
				Toast.makeText(MenuActivity.this, exception.toString(), Toast.LENGTH_LONG).show();
			}}
		);
	}
	
	private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {    		
        	if(progressDialog!=null)
        		progressDialog.dismiss();
        }
    };
	
	private SlideoutHelper mSlideoutHelper;

}
